Monte Carlo Integration

The Monte Carlo integration algorithm estimates the value of the integral of a function f(x) by selecting random points in a rectangle that contains the graph of the function (this is an important condition!) [xmin,xmax] x [ymin,ymax] and recording how many of them fall below the curve. The integral is then estimated to be:

Integral = (xmax-xmin)*(ymax-ymin)*(PointsAbove/TotalPoints) + (xmax-xmin)*ymin

(The last term is needed to take into account that ymin may not be zero.)

This simulation allows you to select how many random points to try, to make an automatic fit to the function graph in the Y axis (thus improving the accuracy of the estimation), and also to display the points or not. Be warned that displaying too many points slows down the simulation responsiveness. You can also easily change the function to integrate.

Finally, the program computes the actual value of the integral using a Romberg algorithm, which is usually very accurate. This allows you to see how good or bad the Monte Carlo integration algorithm is.

As a rule of thumb, one can say that for one-dimensional integrals, standard numerical algorithms, such as Romberg, are much more convenient than Monte Carlo. But computing integrals in higher-dimensions is a different story...